﻿using System;
using NUnit.Framework;
using Rhino.Mocks;

namespace NMA.Infrastructure.NHibernate.Tests.Handler
{
    using NMA.Domain.Model.Repository;
    using NMA.Domain.Shared.Specification;
    using NMA.Infrastructure.NHibernate.Service.Event;    
    using NMA.Infrastructure.NHibernate.Service.Handler;
    using NMA.Infrastructure.NHibernate.DomainObject;    

    public class CategoryAuditTest : TestingBase
    {
        private IAuditRepository _auditRepository = null;
        private ISpecification<AuditEvent> _catSpec = null;
        private AuditHandler _handler = null;

        protected override void Before_each_test()
        {
            base.Before_each_test();

            _auditRepository = MockRepository.GenerateMock<IAuditRepository>();
            _catSpec = MockRepository.GenerateMock<ISpecification<AuditEvent>>();
            _handler = new AuditHandler(_auditRepository, _catSpec);
        }

        [Test]
        public void Can_execute_handle_method()
        {
            AuditEvent catEvent = new AuditEvent()
            {
                UserName = "username",
                UserAction = "useraction",
                DateEvent = DateTime.Now
            };

            _catSpec.Expect(x => x.IsSatisfiedBy(Arg<AuditEvent>.Is.Anything)).Return(true);
            _auditRepository.Expect(x => x.Add(Arg<Audit>.Is.Anything));

            _handler.Handle(catEvent);
            _catSpec.VerifyAllExpectations();
            _auditRepository.VerifyAllExpectations();
        }

        protected override void After_each_test()
        {
            base.After_each_test();

            if (_auditRepository != null)
                GC.SuppressFinalize(_auditRepository);
            if (_catSpec != null)
                GC.SuppressFinalize(_catSpec);
            if (_handler != null)
                GC.SuppressFinalize(_handler);
        }
    }
}